#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QPainter>
#include <QDebug>

#define PORT 8888

#define WIDTH 360
#define HEIGHT 640
//#define FRAME_LEN 230400
#define FRAME_LEN 921600
#define TWO_FRAME_LEN 1843200

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{

    ui->setupUi(this);

    frame = new QByteArray;

    tcpSocket = NULL;

    // 监听套接字，指定父对象，让其自动回收空间
    tcpServer = new QTcpServer(this);
    tcpServer->listen(QHostAddress::Any, PORT);

    connect(tcpServer, &QTcpServer::newConnection,
        [=]()
        {
            // 取出建立好连接的套接字
            tcpSocket = tcpServer->nextPendingConnection();

            // 获取对方IP和端口
            QString ip = tcpSocket->peerAddress().toString();
            qint16 port = tcpSocket->peerPort();
//            QString tmp = QString("[%1:%2]:成功连接").arg(ip).arg(port);
//            ui->textEditRead->setText(tmp);
            qDebug()<<"The clien connected ip:"<<ip<<" port: "<<port;

            connect(tcpSocket, &QTcpSocket::readyRead,
                    [=]()
                    {
                          while(1)
                          {
//                              tcpSocket->readAll();
                              // 1、获取两帧的数据
                              QByteArray data = tcpSocket->read(TWO_FRAME_LEN);
                              if (data.size() <= 0)
                              {
                                  break;
                              }

                              // 2、添加到缓存
                              frame->append(data);
                              if (frame->size() < TWO_FRAME_LEN)
                              {
                                  break;
                              }

                              // 3、显示left sonsor图像
                              uint8_t *frameData = (uint8_t *)frame->data();
                              QImage leftImage(frameData, WIDTH, HEIGHT, QImage::Format_RGBX8888);//转换图像
                              QPixmap leftPixmap = QPixmap::fromImage(leftImage);
                              ui->labelLeft->setPixmap(leftPixmap);
                              ui->labelLeft->show();

                              // 4、显示right sensor图像
                              QImage rightImage(frameData+FRAME_LEN, WIDTH, HEIGHT, QImage::Format_RGBX8888);//转换图像
                              QPixmap rightPixmap = QPixmap::fromImage(rightImage);
                              ui->labelRight->setPixmap(rightPixmap);
                              ui->labelRight->show();

                              frame->remove(0, TWO_FRAME_LEN);

                          }
                    }

                    );
        }

        );
}

MainWindow::~MainWindow()
{
    delete ui;
}

#if 0
void MainWindow::paintEvent(QPaintEvent *e)
{

    QPainter painter;
    painter.begin(this); // 指定当前窗口为绘图设备

    qDebug()<<"frame size: "<<frame->size();
    if (frame->size() >= FRAME_LEN)
    {
        QImage image((uint8_t *)frame->data(), 360, 640, QImage::Format_Grayscale8);//转换图像
        QPixmap pixmap = QPixmap::fromImage(image);
        painter.drawPixmap(0, 0, 360, 640, pixmap);

        frame->clear();
        qDebug()<<"frame clear after size: "<<frame->size();
    }

    painter.end();
}
#endif
